<?php

use think\Db;

//查询购买的代理商等级
function select_agent_type($money){
	switch ($money) {
        case $money >=5000 && $money<=9999:
            $type = 1;
        break;
        case $money >=10000 && $money<=14999:
            $type = 2;
        break;
        case $money >=15000 && $money<=19999:
            $type = 3;
        break;
        case $money >=20000 && $money<=29999:
            $type = 4;
        break;
        case $money >=30000:
            $type = 5;
        break;
    }
    return $type;
}

//会员升级
function user_up($uid,$type,$money){
	update_hy_type($uid,$type);//修改会员等级
	insert_hy_user_log($uid,$type);//添加会员升级记录
	gouwufanli($uid,$type,$money);
	switch ($type) {
		case '2':
			//为上级添加人数
			$tj_uid = $uid;
			do {
				$tj_uid = get_tj_my_userid($tj_uid);
				updata_user_num($tj_uid);
				updata_user_vip_num($tj_uid);
			} while ($tj_uid != 0);
		break;
		case '3':
			$tj_uid = $uid;
			do {
				$tj_uid = get_tj_my_userid($tj_uid);
				updata_user_num($tj_uid);
				updata_user_zhur_num($tj_uid);
				if(get_is_zr_level($tj_uid)){
					
				}
			} while ($tj_uid != 0);
		break;
		case '4':
			$tj_uid = $uid;
			do {
				$tj_uid = get_tj_my_userid($tj_uid);
				updata_user_jinli_num($tj_uid);
			} while ($tj_uid != 0);
		break;
		case '5':
			$tj_uid = $uid;
			do {
				$tj_uid = get_tj_my_userid($tj_uid);
				updata_user_zongj_num($tj_uid);
			} while ($tj_uid != 0);
		break;
	}
	
}

//购物返现
function gouwufanli($uid,$money){//type会员类型 $money金额
	$is_shop = get_user_is_shop($uid);//会员是否收购
	//上级用户邀请奖
	$tj_uid = get_tj_my_userid($uid);
	if($tj_uid){
		$zg_money=($is_shop == 0)?100:$money*0.2;
		$msg="获得余额".$zg_money;
		update_tj_je($tj_uid,$zg_money);
		insert_account_log($uid,$tj_uid,$msg,$zg_money,'1');
	}

	//上上级用户邀请奖
	$tj_uid_2 = get_tj_my_userid($tj_uid);
	if($tj_uid_2){
		$zg_money=($is_shop == 0)?50:$money*0.1;
		$msg="获得余额".$zg_money;
		update_tj_je($tj_uid_2,$zg_money);
		insert_account_log($uid,$tj_uid_2,$msg,$zg_money,'1');
	}

	//主任绩效奖
	$zr_uid = $uid;
	do {
		$zr_uid = get_tj_my_userid($zr_uid);
		$id = get_is_zr_level($zr_uid)?3:0;
	} while ($id != 3 && $zr_uid != 0);
	if($zr_uid){
		$zg_money=$money*0.06;
		$msg="获得余额".$zg_money;
		update_tj_je($zr_uid,$zg_money);
		insert_account_log($uid,$zr_uid,$msg,$zg_money,'2');

		//主任平级奖
		$p_zr_uid = $zr_uid;
		do {
			$p_zr_uid = get_tj_my_userid($p_zr_uid);
			$id = get_is_zr_level($p_zr_uid)?3:0;
		} while ($id != 3 && $p_zr_uid != 0);
		if($p_zr_uid){
			$zg_money=$money*0.1;
			$msg="获得余额".$zg_money;
			update_tj_je($p_zr_uid,$zg_money);
			insert_account_log($uid,$p_zr_uid,$msg,$zg_money,'3');
		}
	}

	//经理绩效奖
	$jl_uid = $uid;
	do {
		$jl_uid = get_tj_my_userid($jl_uid);
		$id = get_user_hy_level($jl_uid);
	} while ($id != 4 && $jl_uid != 0);
	if($jl_uid){
		$zg_money=$money*0.08;
		$msg="获得余额".$zg_money;
		update_tj_je($jl_uid,$zg_money);
		insert_account_log($uid,$jl_uid,$msg,$zg_money,'2');

		//经理平级奖
		$p_jl_uid = $uid;
		do {
			$p_jl_uid = get_tj_my_userid($p_jl_uid);
			$id = get_user_hy_level($p_jl_uid);
		} while ($id != 4 && $p_jl_uid != 0);
		if($p_jl_uid){
			$zg_money=$money*0.13;
			$msg="获得余额".$zg_money;
			update_tj_je($p_jl_uid,$zg_money);
			insert_account_log($uid,$p_jl_uid,$msg,$zg_money,'3');
		}
	}

	//总监绩效奖
	$zj_uid = $uid;
	do {
		$zj_uid = get_tj_my_userid($zj_uid);
		$id = get_user_hy_level($zj_uid);
	} while ($id != 5 && $zj_uid != 0);
	if($zj_uid){
		$zg_money=$money*0.1;
		$msg="获得余额".$zg_money;
		update_tj_je($zj_uid,$zg_money);
		insert_account_log($uid,$zj_uid,$msg,$zg_money,'2');

		//总监平级奖
		$p_zj_uid = $uid;
		do {
			$p_zj_uid = get_tj_my_userid($p_zj_uid);
			$id = get_user_hy_level($p_zj_uid);
		} while ($id != 5 && $p_zj_uid != 0);
		if($p_zj_uid){
			$zg_money=$money*0.15;
			$msg="获得余额".$zg_money;
			update_tj_je($p_zj_uid,$zg_money);
			insert_account_log($uid,$p_zj_uid,$msg,$zg_money,'3');
		}
	}
} 

//代理升级
function user_up_agent($uid,$type,$money){
	$user_type = get_user_hy_agent($uid);

	if($type > $user_type)
		update_hy_agent_type($uid,$type);//修改会员等级

	//上级用户邀请奖
	$tj_uid = get_tj_my_userid($uid);
	if($tj_uid){
		$user_agent_type = get_user_hy_agent($tj_uid);
		if($user_agent_type > 0){
			$zg_money=$money*0.1;
			$msg="获得余额".$zg_money;
			update_tj_je($tj_uid,$zg_money);
			insert_account_log($uid,$tj_uid,$msg,$zg_money,'5');
		}
	}
}
//判断惠员类型
function get_user_hy_level($user_id){
	$user_hy_level=Db::name('user')->where('id',$user_id)->value('level_id');
	return $user_hy_level;
}

//判断惠员类型
function get_user_hy_agent($user_id){
	$user_hy_agent=Db::name('user')->where('id',$user_id)->value('agent_id');
	return $user_hy_agent;
}

//增加总人数
function updata_user_num($user_id){
	Db::name('user_info')->where('user_id',$user_id)->setInc('all_num');
}

//增加vip人数
function updata_user_vip_num($user_id){
	Db::name('user_info')->where('user_id',$user_id)->setInc('vip_num');
}

//增加主任人数
function updata_user_zhur_num($user_id){
	Db::name('user_info')->where('user_id',$user_id)->setInc('director_num');
}

//增加经理人数
function updata_user_jinli_num($user_id){
	Db::name('user_info')->where('user_id',$user_id)->setInc('manager_num');
}

//增加总监人数
function updata_user_zongj_num($user_id){
	Db::name('user_info')->where('user_id',$user_id)->setInc('majordomo_num');
}

//获得团队人数
function get_user_num($user_id){
	$user_num=Db::name('user_info')->where('user_id',$user_id)->value('all_num');
	return $user_num;
}

//判断用户是否满足主任
function get_is_zr_level($user_id){
	$data=Db::name('user_info')->where('user_id',$user_id)->field('straight_num,all_num')->find();
	if($data['straight_num'] >= 6 && $data['all_num'] >= 30){
		return true;
	}else{
		return false;
	}
}

//判断是否购物
function get_user_is_shop($user_id){
	$user_is_shop=Db::name('user')->where('id',$user_id)->value('is_shop');
	return $user_is_shop;
}

//获取推荐用户id
function get_tj_my_userid($user_id){
	$tj_my_userid=Db::name('user')->alias("a")->where('id',$user_id)->join("ysy_user_info b","a.id = b.user_id","LEFT")->value('b.parent_id');
	return $tj_my_userid;
}

//修改惠员的类型
function update_hy_type($user_id,$user_type,$user_money=0){
	Db::name('user')->where('id',$user_id)->update(['level_id' => $user_type,'is_shop' => '1']);
}

//修改代理等级
function update_hy_agent_type($user_id,$user_type,$user_money=0){
	Db::name('user')->where('id',$user_id)->update(['agent_id' => $user_type]);
}

//修改推荐人的金额
function update_tj_je($user_id,$user_money){
	Db::execute("update ysy_user_info set money = money+$user_money where user_id = $user_id");
}

//插入升级记录表
function insert_hy_user_log($user_id,$type){
	$time=time();
	Db::execute("insert into ysy_user_sj (user_id,level_id,add_time) values ($user_id, $type,$time)");
}

//插入记录流水表
function insert_account_log($user_id,$beneficiary_id,$msg,$money,$reason){
	$time =time();
	//记录流水表
	Db::execute("insert into ysy_addhyj_log (user_id,beneficiary_id,time,msg) values ('$user_id', '$beneficiary_id','$time','$msg')");
	//记录总流水表
	$type = get_user_hy_level($user_id);
	Db::execute("insert into ysy_account_log (user_id,reason_id,money_id,money,add_time,other,level_id) values ('$beneficiary_id', '$reason','1','+{$money}','$time','$user_id','$type')");
}

//云仓余额扣除
function updata_yc_je($order_id){
	$time =time();
	$order = Db::name('up_goods')->field('amount,depot_id')->where('id',$order_id)->find();
	$amount = $order['amount'];
	$depot_id = $order['depot_id'];
	Db::execute("update ysy_depot set money = money-$amount,amount = amount+$amount where id = $depot_id");
	Db::execute("update ysy_up_goods set status = 2,z_time = $time where id = $order_id");
}

//获取团队信息
function select_team($user_id){
	$team=Db::name('user')->alias("a")
			->join("ysy_user_info b","a.id = b.user_id","LEFT")
			->join("ysy_level c","a.level_id = c.id","LEFT")
			->where('b.parent_id',$user_id)
			->select();
	return $team;
}